<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>IfWinActive / IfWinNotActive / WinActive</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<link href="../static/theme.css" rel="stylesheet" type="text/css" />
<script src="../static/content.js" type="text/javascript"></script>
</head>
<body>

<h1>IfWinActive / IfWinNotActive / WinActive</h1>

<p>Checks if the specified window exists and is currently active (foremost). If it is, <em>WinActive()</em> returns its Unique ID (HWND).</p>

<pre class="Syntax">IfWinActive [, WinTitle, WinText,  ExcludeTitle, ExcludeText]
IfWinNotActive [, WinTitle, WinText, ExcludeTitle, ExcludeText]
<a href="WinGet.htm">UniqueID</a> := WinActive(&quot;WinTitle&quot;, &quot;WinText&quot;, &quot;ExcludeTitle&quot;, &quot;ExcludeText&quot;)</pre>
<h3>Parameters</h3>
<dl>

  <dt>WinTitle</dt>
  <dd><p>A window title or other criteria identifying the target window. See <a href="../misc/WinTitle.htm">WinTitle</a>.</p></dd>

  <dt>WinText</dt>
  <dd><p>If present, this parameter must be a substring from a single text element of the target window (as revealed by the included Window Spy utility). Hidden text elements are detected if <a href="DetectHiddenText.htm">DetectHiddenText</a> is ON.</p></dd>

  <dt>ExcludeTitle</dt>
  <dd><p>Windows whose titles include this value will not be considered. Note: Due to backward compatibility, <em>IfWinActive</em> and <em>IfWinNotActive</em> interpret this parameter as a command if it exactly matches the name of a command. To work around this, use the <em>WinActive()</em> function instead.</p></dd>

  <dt>ExcludeText</dt>
  <dd><p>Windows whose text include this value will not be considered.</p></dd>

</dl>

<h3>Remarks</h3>
<p>If all parameters are omitted, the <a href="../misc/WinTitle.htm#LastFoundWindow">Last Found Window</a> will be used.</p>
<p>If either of these commands determines that the active window is a qualified match, the <a href="../misc/WinTitle.htm#LastFoundWindow">Last Found Window</a> will be updated to be the active window. In other words, if <em>IfWinActive</em> evaluates to &quot;true&quot; or <em>IfWinNotActive</em> evaluates to &quot;false&quot;, the <a href="../misc/WinTitle.htm#LastFoundWindow">Last Found Window</a> will be updated.</p>
<p>The function WinActive() returns the <a href="WinGet.htm">Unique ID (HWND)</a> of the active window if it matches the specified criteria. If it does not, the function returns 0. Since all non-zero numbers are seen as &quot;true&quot;, the statement <code>if WinActive(&quot;WinTitle&quot;)</code> is true whenever <em>WinTitle</em> is active.</p>
<p>An easy way to retrieve the unique ID of the active window is with <code>ActiveHwnd := WinExist(&quot;A&quot;)</code>.</p>
<p><a href="SetWinDelay.htm">SetWinDelay</a> does not apply to IfWinExist/IfWinActive.</p>
<p>Window titles and text are case sensitive. Hidden windows are not detected unless <a href="DetectHiddenWindows.htm">DetectHiddenWindows</a> has been turned on.</p>
<h3>Related</h3>
<p><a href="WinExist.htm">IfWinExist</a>, <a href="SetTitleMatchMode.htm">SetTitleMatchMode</a>, <a href="DetectHiddenWindows.htm">DetectHiddenWindows</a>, <a href="../misc/WinTitle.htm#LastFoundWindow">Last Found Window</a>, <a href="WinActivate.htm">WinActivate</a>, <a href="WinWaitActive.htm">WinWaitActive</a>, <a href="WinWait.htm">WinWait</a>, <a href="WinWaitClose.htm">WinWaitClose</a>, <a href="_IfWinActive.htm">#IfWinActive/Exist</a></p>
<h3>Example</h3>
<pre class="NoIndent">IfWinActive, Untitled - Notepad
{
    WinMaximize  <em>; Maximizes the Notepad window found by IfWinActive above.</em>
    Send, Some text.{Enter}
    return
}

if WinActive(&quot;ahk_class Notepad&quot;) or WinActive(&quot;ahk_class&quot; . ClassName)  <em>; &quot;ahk_class&quot; need not have a space after it.</em>
    WinClose  <em>; Uses the <a href="../misc/WinTitle.htm#LastFoundWindow">last found window</a>.</em></pre>

</body>
</html>
